//
//  todayView.m
//  pb
//
//  Created by 千翼网络科技 on 2017/5/5.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "todayView.h"
#import "CardLayOut.h"
#import "todayCollectionViewCell.h"

#import "planDetailsViewController.h"

@implementation todayView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = getColor(@"ffffff");
        [self initView];
        [self loginIs];
    }
    
    return self;
}

- (void)loginIs{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Login_is
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self requestData];
                                       
                                   }else{
                                       
                                       [PublicFunction showAlert:nil message:returnData[@"msg"] duration:10.0f];
                                       
                                       loginViewController *loginVc = [[loginViewController alloc]init];
                                       [((SchedulingViewController *)self.vc) presentViewController:loginVc animated:YES completion:nil];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)requestData{
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Today_Message
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       _dataDic = [[NSMutableDictionary alloc]init];
                                       _dataDic = returnData;
                                       
                                       if ([_dataDic[@"data"] count] == 1) {
                                           
                                           _collectionView.frame = CGRectMake( screenScale(80), screenScale(215), scrWidth(), screenScale(600));
                                       }
                                       
                                       [_collectionView reloadData];
                                       
                                       _monthLabel.text = returnData[@"month"];
                                       _dayLabel.text = returnData[@"day"];
                                       
                                   }else{
                                       
                                       _promptLabel.hidden = NO;
                                       _collectionView.hidden = YES;
                                       
                                       _monthLabel.text = returnData[@"month"];
                                       _dayLabel.text = returnData[@"day"];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)initView{
    
    _promptLabel = [[UILabel alloc]init];
    _promptLabel.hidden = YES;
    _promptLabel.text = @"今天可以休息啦～";
    _promptLabel.font = get4sFont(14);
    _promptLabel.textAlignment = NSTextAlignmentCenter;
    _promptLabel.textColor = getMainColor();
    [self addSubview:_promptLabel];
    [_promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(scrHeight()/2 - 64);
        make.left.mas_equalTo(self).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(30));
    }];

    _dateImg = [[UIImageView alloc]init];
    _dateImg.image = [UIImage imageNamed:@"date_plan"];
    [self addSubview:_dateImg];
    [_dateImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(screenScale(40));
        make.left.mas_equalTo(self).with.offset(screenScale(250));
        make.width.mas_equalTo(screenScale(140));
        make.height.mas_equalTo(screenScale(140));
    }];
    
    _monthLabel = [[UILabel alloc]init];
//    _monthLabel.text = @"May";
    _monthLabel.font = get4sFont(11);
    _monthLabel.textAlignment = NSTextAlignmentCenter;
    _monthLabel.textColor = getColor(@"ffffff");
    [self addSubview:_monthLabel];
    [_monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_dateImg).with.offset(screenScale(5));
        make.left.mas_equalTo(_dateImg).with.offset(0);
        make.width.mas_equalTo(screenScale(140));
        make.height.mas_equalTo(screenScale(40));
    }];
    
    _dayLabel = [[UILabel alloc]init];
//    _dayLabel.text = @"2";
    _dayLabel.font = get4sFont(30);
    _dayLabel.textAlignment = NSTextAlignmentCenter;
    _dayLabel.textColor = getColor(@"ffffff");
    [self addSubview:_dayLabel];
    [_dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_dateImg).with.offset(0);
        make.left.mas_equalTo(_dateImg).with.offset(0);
        make.width.mas_equalTo(screenScale(140));
        make.height.mas_equalTo(screenScale(100));
    }];
    
    //
    
    CardLayOut *layout = [CardLayOut new];
    layout.scale = 1.1;
    layout.itemSize = CGSizeMake(screenScale(410), screenScale(515));
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, screenScale(215), scrWidth(), screenScale(600)) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = getColor(@"ffffff");
    [self.collectionView registerClass:[todayCollectionViewCell class] forCellWithReuseIdentifier:@"CellID"];
    [self addSubview:self.collectionView];
}

#pragma mrak ----------------- UICollectionView -------------------

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    planDetailsViewController *settingVC = [[planDetailsViewController alloc]init];
    settingVC.hidesBottomBarWhenPushed = YES;
    settingVC.idStr = _dataDic[@"data"][indexPath.row][@"id"];
    [((SchedulingViewController *)self.vc).navigationController pushViewController:settingVC animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_dataDic[@"data"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    todayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
    cell.backgroundColor = getMainColor();
    cell.layer.cornerRadius = screenScale(10);
    
    cell.numberLabel.text = _dataDic[@"data"][indexPath.row][@"name"];
    
    cell.startLocationLabel.text = _dataDic[@"data"][indexPath.row][@"start_city"];
    cell.startAirportLabel.text = _dataDic[@"data"][indexPath.row][@"start_airport"];
    cell.startTimeLabel.text = _dataDic[@"data"][indexPath.row][@"start_time"];
    
    cell.allTimeLabel.text = _dataDic[@"data"][indexPath.row][@"time"];
    
    cell.endLocationLabel.text = _dataDic[@"data"][indexPath.row][@"end_city"];
    cell.endAirportLabel.text = _dataDic[@"data"][indexPath.row][@"end_airport"];
    cell.endTimeLabel.text = _dataDic[@"data"][indexPath.row][@"end_time"];
    
    cell.messageTextView.text = _dataDic[@"data"][indexPath.row][@"content"];
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:CGPointMake(scrollView.contentOffset.x+0.5*scrollView.bounds.size.width, 0.5*scrollView.bounds.size.height)];
    if (!indexPath || self.currentIndexPath == indexPath) {
        return;
    }
    self.currentIndexPath = indexPath;
    
}

@end
